package com.syn;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.BeanUtilsBean;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.converters.DateConverter;

import java.util.Date;
import java.util.List;
import java.util.Set;

/**
 * 冗余数据同步管理类
 */
public class RedundantDataUpdateMgr {

    /**
     * 初始化表单冗余数据
     */
    public Object initRedundantData(String formId, String actionId, Object destObject) {
        List<FormMapping> formMappingList
                = FormMappingMgr.getMapping(formId, actionId, FormMapping.MAPPING_TYPE_INIT);
        for (FormMapping formMapping : formMappingList) {
            List objectList = queryObjects(destObject, formMapping);
            for (Object srcObject : objectList) {
                porpulateFields(formMapping, srcObject, destObject);
            }
        }
        return destObject;
    }

    /**
     * 同步更新表单冗余数据
     */
    public void updateRedundantData(String formId, String actionId, Object srcObject) {
        List<FormMapping> formMappingList
                = FormMappingMgr.getMapping(formId, actionId, FormMapping.MAPPING_TYPE_UPDATE);
        for (FormMapping formMapping : formMappingList) {
            List objectList = queryObjects(srcObject, formMapping);
            for (Object destObject : objectList) {
                porpulateFields(formMapping, srcObject, destObject);
//                MockDAOService.update(destObject);
            }
        }
    }

    /**
     * 根据和当前的 formObject 和关联关系，查找对应的数据对象
     * 1.init: 查找数据来源对象
     * 2.update: 查找更新目标对象
     */
    private List queryObjects(Object object, FormMapping formMapping) {
        Object queryExampleObject = null;
        if (FormMapping.MAPPING_TYPE_INIT.equals(formMapping.getMappingType())) {
//            queryExampleObject = formMapping.getSrcForm().createFormObject();
        } else if (FormMapping.MAPPING_TYPE_UPDATE.equals(formMapping.getMappingType())) {
//            queryExampleObject = formMapping.getDestForm().createFormObject();
        } else {
            throw new RuntimeException("映射类型错误！");
        }
       /* Set<String> queryFields = formMapping.getQueryFields();
        for (String queryField : queryFields) {
            String dataField = formMapping.getQueryDataField(queryField);
            copyProperty(object, dataField, queryExampleObject, queryField);
        }
        return MockDAOService.findByExmaple(queryExampleObject);*/
        return null;
    }

    /**
     * 对映射字段赋值
     * 经过 Java 反射机制来实现对字段的自动赋值
     */
    private void porpulateFields (FormMapping formMapping, Object srcObject, Object destObject) {
      /*  Set<String> srcFields = formMapping.getSrcFields();
        for (String srcField : srcFields) {
            String destField = formMapping.getDestField(srcField);
            copyProperty(srcObject, srcField, destObject, destField);
        }*/
    }

    /**
     * 对单个字段赋值
     */
    public void copyProperty(Object srcObject, String srcField, Object destObject, String destField) throws Exception {
       /* BeanUtilsBean.setInstance(newBeanUtilsBean2());
        DateConverter dateConverter = newDateConverter();
        dateConverter.setPattern("yyyy-MM-dd HH:mm:ss");
        ConvertUtils.register(dateConverter, Date.class);*/
        BeanUtils.setProperty(destObject, destField, BeanUtils.getProperty(srcObject, srcField));
    }


}
